Attribute VB_Name = "XBRLXL"
'Copyright [2012] [Fundamental X]

'   Licensed under the Apache License, Version 2.0 (the "License");
'   you may not use this file except in compliance with the License.
'   You may obtain a copy of the License at

'       http://www.apache.org/licenses/LICENSE-2.0

'   Unless required by applicable law or agreed to in writing, software
'   distributed under the License is distributed on an "AS IS" BASIS,
'   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'   See the License for the specific language governing permissions and
'   limitations under the License.
Public f_inRoot As String, f_outRoot As String
Public items() As XbrlRefItem
Dim docTot As Integer
Dim fCmpNo As Integer
Dim outputName As String

Dim docs() As XbrlDoc
Sub Main()

'On Error GoTo ErrorHandler

f_outRoot = somePath

'loadItems


getFiling Command


getFiles
docs(1).initDOM
docs(1).ttrw4
docs(1).ssqu7
docs(1).trace2
docs(1).bbgggt5
docs(1).uuflii1
docs(1).postRefProcessing


If docs(1).itemTot > 3 Then
    savePreToSheet
    log docs(1).f_name & "successfully created"
    Exit Sub
End If

Err.Raise 999, "XBRL.Main", "Insufficient Items found in filing"

ErrorHandler:
If docs(1).f_name = "" Then 'Error may occur before filing has been found in the database.
    log Command & "; " & Err.Number & "; " & Err.Description & "; " & Err.Source
Else
    log docs(1).f_name & "; " & Err.Number & "; " & Err.Description & "; " & Err.Source
End If


End Sub

Sub loadItems()

Dim currLine As String
Dim currFields() As String
Dim i As Integer

Open LocData.DRIVE & LocData.TAGFILE For Input As #1

Line Input #1, currLine

ReDim items(CInt(currLine)) As XbrlRefItem

For i = 1 To UBound(items)
    Line Input #1, currLine
    currFields = Split(currLine, Chr(9))
    Set items(i) = New XbrlRefItem
    items(i).id = currFields(0)
    items(i).tag = currFields(1)
Next i

Close #1


End Sub
Sub getFiling(args As String)

Dim argArray() As String
Dim nxtArg As Integer
Dim prevArg As Integer
Dim filingsConn As ADODB.Connection
Dim filing As ADODB.Recordset
Dim currANo As String


argArray = Split(args)

fCmpNo = argArray(0)
currANo = argArray(1)
outputName = argArray(2)


ReDim docs(1) As XbrlDoc
Set docs(1) = New XbrlDoc
docs(1).initRefItems

Set filingsConn = New ADODB.Connection
filingsConn.ConnectionString = "Provider=sqloledb;" & "Data Source=" & LocData.DATASRC & "Initial Catalog=someXBRLFilingCat;" & "Integrated Security=SSPI;"
filingsConn.Open

Set filing = New ADODB.Recordset
filing.Open "Select * from someFilingList where AccessionNo=" & currANo, filingsConn
docs(1).addFiling filing
filing.Close

End Sub
Sub getFilingByTkrDate(tkr As String, d As Long)



End Sub
Sub getFiles()

Dim fs As Object
Dim http As Object
Dim oStream As ADODB.Stream
Dim URL As String
Dim zip_name As String
Dim objZip As Object

URL = docs(1).filing.edgarURLBody & "-xbrl.zip"
zip_name = docs(1).filing.accessionNo & ".zip"


Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.fileExists(f_outRoot & zip_name) Then
    Set http = CreateObject("Microsoft.XMLHTTP")
    http.Open "GET", URL, False
    http.Send

    Set oStream = New ADODB.Stream
    oStream.Type = ADODB.adTypeBinary
    oStream.Open
    oStream.Write http.responseBody

        oStream.SaveToFile f_outRoot & zip_name, ADODB.adSaveCreateOverWrite

    oStream.Close

    Set oStream = Nothing
    Set http = Nothing
End If

Set objZip = CreateObject("SomeZipProgram")
objZip.UnPack f_outRoot & zip_name, f_outRoot & docs(1).filing.accessionNo
Set objZip = Nothing

End Sub
Sub savePreToSheet()

Dim fs As Object
Dim f_outputPath As String
Dim excelConn As ADODB.Connection
Dim xbrlSheet As ADODB.Recordset
Dim currItemNo As Integer, currSectPreNo As Integer
Dim currRefItems() As XbrlRefEle
Dim currItems() As XbrlElement
Dim currSections() As Obj1
Dim currPreRels() As XbrlPreRel
Dim currVal As Variant
Dim i As Integer, j As Integer, k As Integer

Set fs = CreateObject("Scripting.FileSystemObject")
f_outputPath = f_outRoot & outputName & ".xls"
If Not fs.fileExists(f_outputPath) Then
    fs.CopyFile LocData.DRIVE & LocData.XLTEMPLATE, f_outputPath, True
End If
Set fs = Nothing

Set excelConn = New ADODB.Connection
With excelConn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Properties("Extended Properties").value = "Excel 8.0;HDR=No"
    .Open f_outputPath
End With


currSheet = "Filing (" & fCmpNo & ")"
Set xbrlSheet = New ADODB.Recordset
xbrlSheet.Open "SELECT * FROM [" & currSheet & "$A2:E2]", excelConn, adOpenForwardOnly, adLockPessimistic
For i = 1 To 3
    xbrlSheet.Fields(1 + i).value = Year(docs(1).getPeriodToXLS(i * 2).endDate)
Next i
xbrlSheet.MoveNext
currRefItems = docs(1).getRefItems

Set xbrlSheet = New ADODB.Recordset
xbrlSheet.Open "SELECT * FROM [" & currSheet & "$A5:C15]", excelConn, adOpenForwardOnly, adLockPessimistic
For i = 1 To 11
    xbrlSheet.Fields(0).value = currRefItems(i).schema
    xbrlSheet.Fields(1).value = currRefItems(i).tag
    xbrlSheet.Fields(2).value = currRefItems(i).value
    xbrlSheet.MoveNext
Next i

Set xbrlSheet = New ADODB.Recordset
xbrlSheet.Open "SELECT * FROM [" & currSheet & "$A16:C20]", excelConn, adOpenForwardOnly, adLockPessimistic
For i = 12 To 16
    xbrlSheet.Fields(0).value = currRefItems(i).schema
    xbrlSheet.Fields(1).value = currRefItems(i).tag
    xbrlSheet.Fields(2).value = currRefItems(i).value
    xbrlSheet.MoveNext
Next i

currSections = docs(1).getSections
currItems = docs(1).getItems
Set xbrlSheet = New ADODB.Recordset

currItmRge = "ITEMS_" & fCmpNo
xbrlSheet.Open "SELECT * FROM [" & currItmRge & "]", excelConn, adOpenForwardOnly, adLockPessimistic

For i = 1 To UBound(currSections)
    xbrlSheet.AddNew
    xbrlSheet.Fields(1).value = UCase(currSections(i).stdName)
    xbrlSheet.Update
    currPreRels = currSections(i).preRels
    For j = 1 To UBound(currPreRels)
        currItemNo = currPreRels(j).itemNo
        If currItemNo > 0 Then
            xbrlSheet.AddNew
            xbrlSheet.Fields(0).value = currItems(currItemNo).schema
            xbrlSheet.Fields(1).value = currPreRels(j).tag
            For k = 1 To 3
                currVal = currItems(currItemNo).value(k)
                If IsNumeric(currVal) Then
                    If Not IsEmpty(currVal) Then
                        If Len(currVal) = 0 Then
                            currVal = 0
                        End If
                        xbrlSheet.Fields(k + 1).value = currVal
                    End If
                End If
            Next k
            xbrlSheet.Update
        End If
    Next j
Next i
xbrlSheet.Close
excelConn.Close



End Sub

Sub log(msg As String)

Dim fs As Object, outStream As Object

Set fs = CreateObject("Scripting.FileSystemObject")
Set outStream = fs.OpenTextFile(LocData.DRIVE & someDirectory & LocData.LOGS & LocData.LOGFILE, 8, True)
outStream.Write msg & vbCrLf
outStream.Close
Set fs = Nothing

End Sub
Function convLongDate(d As Long)

dStr = Left$(d, 4) & "-" & Mid$(d, 5, 2) & "-" & Right$(d, 2)
convLongDate = CDate(dStr)

End Function

